package com.simdo.g768.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.simdo.g768.Mqtt.MqttMsg;
import com.simdo.g768.Mqtt.MqttUeBean;
import com.simdo.g768.R;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class MqttInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final List<MqttMsg> dataList;
    private final Context context;

    public MqttInfoAdapter(Context context) {
        this.dataList = new ArrayList<>();
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup arg0, int arg1) {
        if (-1 == arg1)
            return new EmptyHolder(LayoutInflater.from(context).inflate(R.layout.item_empty, arg0, false));

        View item = LayoutInflater.from(context).inflate(R.layout.item_mqtt_info, arg0, false);
        return new ViewHolder(item);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int pos) {

        if (holder instanceof EmptyHolder) return;

        final ViewHolder viewHolder = (ViewHolder) holder;
        final MqttUeBean item = dataList.get(pos).getMqttUeBean();
        viewHolder.tv_time.setText(String.valueOf(item.getTime()));
        viewHolder.tv_imsi.setText(String.valueOf(item.getImsi()));
        viewHolder.tv_rsrp.setText(MessageFormat.format("-{0}", item.getRsrp()));

    }
    long startTime, endTime;
    public void setTime(long startTime, long endTime){
        this.startTime = startTime;
        this.endTime = endTime;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void updateDataList(List<MqttMsg> dataList){
        this.dataList.clear();
        for (MqttMsg mqttMsg : dataList) {
            long longTime = mqttMsg.getMqttUeBean().getLongTime();
            if (longTime >= startTime && longTime <= endTime) this.dataList.add(mqttMsg);
        }
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void addData(MqttMsg mqttMsg){
        long longTime = mqttMsg.getMqttUeBean().getLongTime();
        if (longTime >= startTime && longTime <= endTime) this.dataList.add(mqttMsg);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return !dataList.isEmpty() ? dataList.size() : 1;//不同点4,也是关键点
    }

    @Override
    public int getItemViewType(int position) {
        if (dataList.isEmpty()) return -1;
        return super.getItemViewType(position);
    }

    /**
     * 空数据显示的   不同点1
     */
    static class EmptyHolder extends RecyclerView.ViewHolder {

        public EmptyHolder(View itemView) {
            super(itemView);
        }

    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_time, tv_imsi, tv_rsrp;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_imsi = itemView.findViewById(R.id.tv_imsi);
            tv_rsrp = itemView.findViewById(R.id.tv_rsrp);
        }
    }
}
